package com.message.queue.rocket.consumer;

import com.message.queue.constant.RocketConstant;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * Created by 张波 on 2019/9/22.
 */
public class DefaultProduct {


    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("default_producer");
        producer.setNamesrvAddr(RocketConstant.NAME_SERVER);
        producer.start();

        for (int i = 0; i < 20; ++i) {
            String tags = i % 2 == 0 ? "TagA" : "TagB";
            Message message = new Message(ConsumerParent.TOPIC,  //主题
                    tags, //标签
                    "key" + i,  //唯一标识
                    ("Hello Message" + i).getBytes());  //消息内容
            //1、发送同步消息
            SendResult result = producer.send(message);
            System.out.println("result:" + result);
        }
        //发送异常消息时不能关掉，防止进行回调时发送者已经被关掉了（会报异常）
        producer.shutdown();

    }
}
